package jp.gr.java_conf.homeems.gw.internal.measurementdatamanager;

import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;

import jp.gr.java_conf.homeems.gw.common.exception.GwException;
import jp.gr.java_conf.homeems.gw.measurementdatamanager.MeasurementDataBean;
import jp.gr.java_conf.homeems.gw.measurementdatamanager.MeasurementDataManagerService;

public class MeasurementDataManagerServiceImpl implements MeasurementDataManagerService {
	
	public String saveMeasurementData(MeasurementDataBean measurementDataBean) throws GwException {
		String url = "http://127.0.0.1:8888/GotApp";
		ClientResource resource = new ClientResource(url);

		String result = null;
		try 
		{
			ArrayList<MeasurementDataBean> dataBeans = new ArrayList<MeasurementDataBean>();
			dataBeans.add(measurementDataBean);
			
			JSONArray jsonArray = new JSONArray();
			jsonArray.put(new JSONObject(measurementDataBean));
			
			JsonRepresentation json = new JsonRepresentation(jsonArray);
			Representation representation = resource.put(json);
			result = representation.getText();
		} 
		catch (ResourceException e) {
			Exception originalException = e;
			throw (GwException)originalException;
		} catch (IOException e) {
			Exception originalException = e;
			throw (GwException)originalException;
		}

		return result;
	}

}
